JavaScript Break and Continue Statements

1. Break Statement


<!DOCTYPE html>
<html lang="en">
<head>    
<body>
   <h2>Break Statement</h2>
   <script>                    
        for (let i = 0; i < 10; i++) {
            if (i === 5) {
                break; // Exit the loop when i is 5
            }
            console.log(i);
        }// Output: 0, 1, 2, 3, 4
    </script>
</body>
</html>                        
                        

2. Continue Statement


<!DOCTYPE html>
<html lang="en">
<head>    
<body>
   <h2>Continue Statement</h2>
   <script>                          
        for (let i = 0; i < 10; i++) {
            if (i === 5) {
                continue; // Skip the iteration when i is 5
            }
            console.log(i);
        }
    </script>
</body>
</html> 
// Output: 0, 1, 2, 3, 4, 6, 7, 8, 9

3. Break in Nested Loops


<!DOCTYPE html>
<html lang="en">
<head>    
<body>
   <h2>Break in Nested Loops</h2>
   <script>                          
        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 3; j++) {
                if (i === 1 && j === 1) {
                    break; // Exit the inner loop when i is 1 and j is 1
                }
                console.log(i, j);
            }
        }
    </script>
</body>
</html> 
// Output: 0 0, 0 1, 0 2, 1 0

4. Continue in Nested Loops


<!DOCTYPE html>
<html lang="en">
<head>    
<body>
   <h2>Break in Nested Loops</h2>
   <script>                         
        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 3; j++) {
                if (j === 1) {
                    continue; // Skip the iteration when j is 1
                }
                console.log(i, j);
            }
        }
      </script>
</body>
</html>
// Output: 0 0, 0 2, 1 0, 1 2, 2 0, 2 2